package sns.dao;

import java.util.List;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import sns.bean.Comment;
import sns.bean.Mblog;

public class CommentDao extends HibernateDaoSupport {

	public void save(Comment comment){
		getSession().save(comment);
	}
	
	public void merge(Comment comment){
		getSession().merge(comment);
	}
	
	public void delete(Comment comment){
		getSession().delete(comment);
	}
	
	/**
	 * get comment by its id
	 * 
	 * @param idcomment
	 * @return
	 */
	public Comment getCommentById(Long idcomment){
		if(idcomment==null){
			return null;
		}
		Query q = getSession().createQuery("select comment from Comment as comment where comment.idcomment=:idcomment");
		q.setLong("idcomment", idcomment);
		return (Comment)q.uniqueResult();
	}
	
	/**
	 * get comments of the mblog
	 * 
	 * @param mblog
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Comment> getCommentOfMblog(Mblog mblog){
		if(mblog==null){
			return null;
		}
		Query q = getSession().createQuery("select comment from Comment as comment where comment.targetMblog.idmblog = :idmblog order by comment.commentTime asc");
		q.setLong("idmblog", mblog.getIdmblog());
		return (List<Comment>)q.list();
	}
}
